package com.cgq.pro.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cgq.base.action.BaseAction;
import com.cgq.pro.entity.Policy;
import com.cgq.pro.service.PolicyService;
import com.cgq.utils.Constants;
import com.cgq.utils.GlobalContext;
import com.cgq.utils.IdGenerator;

/**
 * 控制层组件
 * 国内政策
 * @author chenkaile
 *
 */
@Controller
public class PolicyAction extends BaseAction {
	private static final long serialVersionUID = -4809755022933244903L;
	

	/** 注入国内政策service*/
	@Autowired
	private PolicyService policyService;
	
	private Policy model;
	/** 政策类型：1为国内政策，2为地方政策 */
	private String typeid;
	/** 操作方式：增加(add),删除(delete),修改(modify),详情(detail)*/
	private String viewType;
	/**删除数据的id数组*/
	private String ids;
	/**查询开始时间*/
	private String startDate;
	/**查询结束时间*/
	private String endDate;

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getIds() {
		return ids;
	}
	
	public Policy getModel() {
		return model;
	}

	public void setModel(Policy model) {
		this.model = model;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}
	
	public String getViewType() {
		return viewType;
	}

	public void setViewType(String viewType) {
		this.viewType = viewType;
	}
	
	/**
	 * 国内政策列表界面
	 * @return
	 */
	public String toList(){
		permissionId=getCurrentUser().getPerId().getPerFlag();
		return GlobalContext.RESULT_LIST_UI;
	}
	
	/**
	 * 增删改查界面
	 * @return
	 */
	public String toForm(){
		if(GlobalContext.RESULT_ADD.equals(viewType)){
			model=new Policy();
		}else{
			model=policyService.find(model.getId());
		}
		return GlobalContext.RESULT_FORM_UI;
	}
	
	/**
	 * 保存数据
	 * @throws Exception 
	 */ 
	public void save() throws Exception{
		try {
			Date currentDate=new Date();
			
			model.setType(Integer.parseInt(typeid));
			if(GlobalContext.RESULT_ADD.equals(viewType))//保存
			{
				model.setId(IdGenerator.getId(service,Constants.TABLE_POLICY, "PO", 10));
				model.setUserCre(getCurrentUser().getName());
				model.setDateUpd(model.getDateCre());
				model.setUserUpd(model.getUserCre());
				policyService.save(model);
				ajaxDoneSuccess("保存成功");
			}else{
				model.setDateUpd(currentDate);
				model.setUserUpd(getCurrentUser().getName());
				policyService.update(model);
				ajaxDoneSuccess("更新成功");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneSuccess("保存失败");
		}
	}
	
	public String getAllRecords(){
		  //当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数  
        int number = Integer.parseInt((rows == null || rows == "0") ? "5":rows);  
      //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;
        Map<String, String> map=null;
        if(GlobalContext.RESULT_QUERY.equals(viewType)){ //如果执行查询操作
        	map=new HashMap<String, String>();
        	map.put("startDate", startDate);
        	map.put("endDate",endDate);
        	viewType="";
        }
        int total=policyService.getTotal(map, typeid);
        
        List<Policy> list=policyService.getAllRecords(start, number, map,typeid);
        List<Map<String, Object>> maps=new ArrayList<Map<String,Object>>();
        for (Policy policy : list) {
        	 maps.add(policy.toMap());
		}
        Map<String, Object> json = new HashMap<String, Object>(); 
        json.put("total", total);
        json.put("rows", maps);
        resultObj =JSONObject.fromObject(json);
		return "success";
	}
	
	public void delete() throws Exception{
		try {
			String[] idList=ids.split(",");
			policyService.delete(idList);
			ajaxDoneSuccess("删除成功");
		} catch (Exception e) {
			ajaxDoneSuccess("删除失败");
		}
	}
}
